// 封装axios
import axios from "axios";

const service = axios.create({
    baseURL:'http://localhost:8080',
    timeout:60000
})

// 添加请求拦截器
service.interceptors.request.use(config=>{
    console.log("请求拦截器被执行")
    console.log(config)
    // 可以在所有的请求中添加 token 在请求的头部
    let token = sessionStorage.getItem("token");
    // let token = "111";
    config.headers['token'] = token
    return config;
},error => {
    console.log(error)
    return Promise.reject(error)
})

service.interceptors.response.use(response=>{
    console.log("响应拦截器被执行")
    console.log(response)
    if(response.data.status == false){
        // 证明token无效
        window.location.href = '/login'
    }
    return response
},error => {
    console.log(error)
    return Promise.reject(error)
})

export default service